import { setState } from './index'
import { apiPublish, apiGetPosts, apiDeletePost } from '@/api/posts'

const state = {
  posts: [],
  hasNext: true,
  last: 0,
  myPosts: [],
  myHasNext: true,
  myLast: 0
}

const mutations = { setState }

const actions = {
  publishPost({ rootState, dispatch }, { imageUrl, text }) {
    const token = rootState.Client.token
    return new Promise((resolve, reject) => {
      wx.uploadFile({
        url: 'https://api.hilval.com/upload/images',
        filePath: imageUrl,
        name: 'images',
        success: res => {
          console.log('上传成功', res)
          const url = res.data.match(/\/images.*?g/g)[0]
          console.log({ url })
          apiPublish(token, {
            intros: [{ type: 'image', content: url }, { type: 'text', content: text }]
          }).then(res => {
            if (res.data.data && res.data.data.addUserMicroblog) {
              resolve()
              dispatch('getPosts')
            } else reject()
          })
        },
        fail: () => reject()
      })
    })
  },

  async getPosts({ rootState, commit }) {
    const token = rootState.Client.token
    try {
      const { posts, hasNext, last } = await apiGetPosts(token, {
        page: { pageSize: 10, lastRownum: 0 }
      })
      commit('setState', { posts, hasNext, last })
    } catch (err) {
      return Promise.reject(err)
    }
  },

  async getMorePosts({ rootState, state, commit }) {
    if (!state.hasNext) {
      return Promise.reject(new Error('no more'))
    }
    const token = rootState.Client.token
    try {
      const { posts, hasNext, last } = await apiGetPosts(token, {
        page: { pageSize: 10, lastRownum: state.last }
      })
      commit('setState', { posts: [...state.posts, ...posts], hasNext, last })
      return Promise.resolve()
    } catch (err) {
      return Promise.reject(err)
    }
  },

  async getMyPosts({ rootState, commit }) {
    const token = rootState.Client.token
    try {
      const { posts, hasNext, last } = await apiGetPosts(token, {
        page: { pageSize: 10, lastRownum: 0 },
        isSelf: true
      })
      commit('setState', { myPosts: posts, myHasNext: hasNext, myLast: last })
      return Promise.resolve({ posts, hasNext, last })
    } catch (err) {
      return Promise.reject(err)
    }
  },

  async getMoreMyPosts({ rootState, state, commit }) {
    if (!state.myHasNext) {
      return Promise.reject(new Error('no more'))
    }
    const token = rootState.Client.token
    try {
      const { posts, hasNext, last } = await apiGetPosts(token, {
        page: { pageSize: 10, lastRownum: state.myLast }
      })
      commit('setState', {
        myPosts: [...state.myPosts, ...posts],
        myHasNext: hasNext,
        myLast: last
      })
      return Promise.resolve()
    } catch (err) {
      return Promise.reject(err)
    }
  },

  async deletePost({ rootState, dispatch }, id) {
    const token = rootState.Client.token
    try {
      await apiDeletePost(token, id)
      dispatch('getPosts')
      dispatch('getMyPosts')
      return Promise.resolve()
    } catch (err) {
      return Promise.reject(err)
    }
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
